    #include <bits/stdc++.h>
    using namespace std;
     
     
    vector<int> sign;
     
    int main(int argc, char const *argv[])
    {
        for (int i=1; i<=9; ++i) {
            
            for (int j=1; j<=9; ++j) {
                char s[100];
                int len = 0;
                memset(s, '0' + j, i);
                s[i] = 0;
                sign.push_back(atoi(s));
            }
        }
     
        // for (int x : sign) printf("%d\n", x);
     
        int t;
        scanf("%d", &t);
        for (int kk=0; kk<t; ++kk) {
            int x; scanf("%d", &x);
            printf("%d\n", upper_bound(sign.begin(), sign.end(), x) - sign.begin());
        }
        return 0;
    }